/*
This file generates the results on spillover effects of nonbank lending, 
see Section 6.3 / Appendix D:

	- Table E.5: Industry presence of nonbanks & corporate investment
	- Table E.6: Municipality presence of nonbanks & household consumption
	
*/

		
/////////////////////////////////////////////////////////////////////////////////
///
/// 					Corporate credit market 
///
/////////////////////////////////////////////////////////////////////////////////

		
/////////////////////////////////////////////////////////////////////////////////
/// Table E.5: Industry presence of nonbanks & corporate investment
/////////////////////////////////////////////////////////////////////////////////

use if delinquency != 1 using "${procdata}/URTEVIRK_FIRE_FIRM_03-18.dta", clear 


rename industry_borrower_coarse ind_2d
drop if mi(ind_2d)

** Compute nonbank credit share for every industry-year pair
gegen tot_industry_credit = total(REST_GAELD_BLB), by(ind_2d year)
gegen nonbank_industry_credit = total(REST_GAELD_BLB) if nonbank_lender == 1, by(ind_2d year)

gen nonbank_debtshare_temp = nonbank_industry_credit / tot_industry_credit 
replace nonbank_debtshare_temp = 0 if mi(nonbank_debtshare_temp)

gegen nonbank_debtshare = max(nonbank_debtshare_temp), by(ind_2d year)

* Identify borrowers without nonbank debt
gegen byte aux = max(nonbank_lender), by(id_borrower year)
gen byte bank_borrower = (aux == 0)

* Collapse to firm-year-level 
local macrovars JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
	DK_inflation_rate vix

gcollapse (lastnm) GF_AINV `macrovars' *nonbank_debtshare* ind_2d /// 
	 bank_borrower, by(id_borrower year)

* Standardize nonbank credit share
cap drop std_*
preserve 
gcollapse (lastnm) nonbank_debtshare, by(ind_2d year)
qui egen std_nonbank_debtshare = std(nonbank_debtshare) 
tempfile ind_share
save `ind_share'
restore 
merge m:1 ind_2d year using `ind_share', keep(match) nogen

* Run regressions at firm-year level
xtset id_borrower year
gen ln_ainv = log(GF_AINV)
eststo clear 

local macrovars JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
	DK_inflation_rate vix
	
local reg_model qui reghdfe ln_ainv ///
	cL.std_nonbank_debtshare##cL.(`macrovars'), vce(cl id_borrower)

foreach smpl in full bnk_borrower {
	preserve 
	
	if "`smpl'" == "bnk_borrower" keep if bank_borrower == 1
	
	eststo ols_`smpl': `reg_model' noabsorb
	eststo YearFE_`smpl': `reg_model' a(year)
	eststo YearIndFE_`smpl': `reg_model' a(year ind_2d)
	eststo YearIndFrmFE_`smpl': `reg_model' a(year ind_2d id_borrower)
	
	restore
}

* Export results 
qui estadd local MacroControlsInt "Yes": *
qui estadd local YearFE "Yes": Year*
qui estadd local IndustryFE "Yes": *Ind*
qui estadd local BorrowerFE "Yes": *Frm*

local stats_TestFE N r2  MacroControlsInt  YearFE IndustryFE  BorrowerFE 
local label_TestFE `""Observations" "R2" "Macro Var. Interactions" "Year FE" "Industry FE" "Borrower FE" "'

local options varwidth(25) star(* 0.10 ** 0.05 *** 0.01) collabels(none) ///
		keep(cL.std_nonbank_debtshare#cL.JK_mpshocksign) /// 
		transform((exp(@)-1)*100 100*exp(@)) /// 
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(cL.std_nonbank_debtshare#cL.JK_mpshocksign "Nonbank industry share x MP Shock")  ///
		 nomtitles fragment 
		 
esttab *_full, `options'
esttab *_bnk_borrower, `options'

local filename FIRMS_Regression_spillover_industry_ainv

esttab *_full using "${tables}/`filename'.tex", `options' booktabs ///
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \toprule") ///
		posthead("\hline \\ \multicolumn{2}{l}{\textbf{A. Outcome var: All borrowers}} \\\\[-1ex]") ///
		stats(N r2, labels("Observations" "R2") fmt(%12.0gc 2)) replace

esttab *_bnk_borrower using "${tables}/`filename'.tex", `options' booktabs  ///
		posthead("\\ \multicolumn{2}{l}{\textbf{B. Only bank borrowers}} \\\\[-1ex]") ///
		append nonumbers nolines ///
		prefoot("\hline") postfoot("\hline\hline \end{tabular} }")  ///
		stats(`stats_TestFE', labels(`label_TestFE') fmt(%12.0fc 2)) 
				





/////////////////////////////////////////////////////////////////////////////////
///
/// 					Consumer credit market 
///
/////////////////////////////////////////////////////////////////////////////////

		
/////////////////////////////////////////////////////////////////////////////////
/// Table E.6: Municipality presence of nonbanks & household consumption
/////////////////////////////////////////////////////////////////////////////////

use if delinquency != 1 using "${procdata}/urtepers_panel_ALL.dta", clear 

drop if mi(municipality)

** Compute nonbank credit share for every municipality-year pair
gegen tot_municipality_credit = total(REST_GAELD_BLB), by(municipality year)
gegen nonbank_municipality_credit = total(REST_GAELD_BLB) if nonbank_lender == 1, by(municipality year)

gen nonbank_debtshare_temp = nonbank_municipality_credit / tot_municipality_credit 
replace nonbank_debtshare_temp = 0 if mi(nonbank_debtshare_temp)

gegen nonbank_debtshare = max(nonbank_debtshare_temp), by(municipality year)

* Identify borrowers without nonbank debt
gegen byte aux = max(nonbank_lender), by(id_borrower year)
gen byte bank_borrower = (aux == 0)

* Collapse to household-year-level 
local macrovars JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
	DK_inflation_rate vix

gcollapse (lastnm) famforbrug1 `macrovars' *nonbank_debtshare* municipality /// 
	 bank_borrower, by(id_borrower year)

* Standardize nonbank credit share
cap drop std_*
preserve 
gcollapse (lastnm) nonbank_debtshare, by(municipality year)
qui egen std_nonbank_debtshare = std(nonbank_debtshare) 
tempfile ind_share
save `ind_share'
restore 
merge m:1 municipality year using `ind_share', keep(match) nogen

* Run regressions at household-year level
xtset id_borrower year
gen ln_consumption = log(famforbrug1)
eststo clear 

local macrovars JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
	DK_inflation_rate vix
	
local reg_model qui reghdfe ln_consumption ///
	cL.std_nonbank_debtshare##cL.(`macrovars'), vce(cl id_borrower)

foreach smpl in full bnk_borrower {
	preserve 
	
	if "`smpl'" == "bnk_borrower" keep if bank_borrower == 1
	
	eststo ols_`smpl': `reg_model' noabsorb
	eststo YearFE_`smpl': `reg_model' a(year)
	eststo YearIndFE_`smpl': `reg_model' a(year municipality)
	eststo YearIndFrmFE_`smpl': `reg_model' a(year municipality id_borrower)
	
	restore
}

* Export results 
qui estadd local MacroControlsInt "Yes": *
qui estadd local YearFE "Yes": Year*
qui estadd local municipalityFE "Yes": *Ind*
qui estadd local BorrowerFE "Yes": *Frm*

local stats_TestFE N r2  MacroControlsInt  YearFE municipalityFE  BorrowerFE 
local label_TestFE `""Observations" "R2" "Macro Var. Interactions" "Year FE" "Municipality FE" "Borrower FE" "'

local options varwidth(25) star(* 0.10 ** 0.05 *** 0.01) collabels(none) ///
		keep(cL.std_nonbank_debtshare#cL.JK_mpshocksign) /// 
		transform((exp(@)-1)*100 100*exp(@)) /// 
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(cL.std_nonbank_debtshare#cL.JK_mpshocksign "Nonbank municipality share x MP Shock")  ///
		 nomtitles fragment 
		 
esttab *_full, `options'
esttab *_bnk_borrower, `options'

local filename PERS_Regression_spillover_municipality

esttab *_full using "${tables}/`filename'.tex", `options' booktabs ///
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \toprule") ///
		posthead("\hline \\ \multicolumn{2}{l}{\textbf{A. Outcome var: All borrowers}} \\\\[-1ex]") ///
		stats(N r2, labels("Observations" "R2") fmt(%12.0gc 2)) replace

esttab *_bnk_borrower using "${tables}/`filename'.tex", `options' booktabs  ///
		posthead("\\ \multicolumn{2}{l}{\textbf{B. Only bank borrowers}} \\\\[-1ex]") ///
		append nonumbers nolines ///
		prefoot("\hline") postfoot("\hline\hline \end{tabular} }")  ///
		stats(`stats_TestFE', labels(`label_TestFE') fmt(%12.0fc 2)) 		

		
